<?php

Class Router 
{
	private $registry;
	private $path;
    private $args = array();
	

    function __construct($registry) 
	{
        $this->registry = $registry;
		$Uri = new URI($registry);
		$registry->set ('Uri', $Uri);
    }

	function setPath($path) 
	{
        //$path = trim($path, '/\\');
        $path .= DIRSEP;
		
        if (is_dir($path) == false) {
                throw new Exception ('Invalid controller path: `' . $path . '`');
        }

        $this->path = $path;
	}
	
	private function getRoute()
	{
		$route = (empty($_GET['route'])) ? '' : $_GET['route'];

        if (empty($route)) { $route = 'index'; }
		
		
		
        // Get separate parts
        $route = trim($route, '/\\');
		$parts = explode('/',  $route);
		
		
		
		return $route;
	}
	
	private function getController(&$file, &$controller, &$action, &$args) 
	{
        $parts = explode('/',  $this->getRoute());
		
        // Find right controller
        $cmd_path = $this->path;
        foreach ($parts as $part) {
                $fullpath = $cmd_path . $part;
                       
                // Is there a dir with this path?
                if (is_dir($fullpath)) {
                        $cmd_path .= $part . DIRSEP;
                        array_shift($parts);
                        continue;
                }

                // Find the file
                if (is_file($fullpath . '.php')) {
                        $controller = $part;
                        array_shift($parts);
                        break;
                }
        }

        if (empty($controller)) { $controller = 'index'; };

        // Get action
        $action = array_shift($parts);
        if (empty($action)) { $action = 'index'; }

        $file = $cmd_path . $controller . '.php';
        $args = $parts;
		
	}
	
	private function iniciarController(&$file, &$controller, &$action, &$args) {
		// File available?
        if (is_readable($file) == false) {
                die ('404 Not Found');
        }

        // Include the file
        include ($file);

        // Initiate the class
        $class = 'Controller_' . $controller;
        $controller = new $class($this->registry);

        // Action available?
        if (is_callable(array($controller, $action)) == false) {
                die ('404 Not Found');
        }

        // Run action		
        $controller->$action();

	}
	
	private function iniciarScaffold(){
		//a query string deve obedecer ao modelo ?route=scaffold/[nome tablea]/[comando]
		$scaffold_args = explode('/',  $this->getRoute());
		if( isset($scaffold_args[1]) ){
			$scaffold = new Scaffold($this->registry, $scaffold_args[1]);
		}
		else
		{
			$scaffold = new Scaffold($this->registry, 'null');
		}
		
		if( (isset($scaffold_args[2])) &&  (is_callable(array($scaffold, $scaffold_args[2]))) ){
			$executa = $scaffold_args[2];
		}
		else
		{
			$executa = 'listar';
		}
		$scaffold->$executa();
	}
	
	public function getPath(){
		return $this->path;
	}
	
	function delegate() 
	{
        // Analyze route
        $this->getController($file, $controller, $action, $args);
		
		$scaffold_flag = explode('/',  $this->getRoute());
	
		if( $scaffold_flag[0] === 'scaffold' and $this->registry['Scaffold_activo'] === true)
		{
			$this->iniciarScaffold();
			return;
		}
		
		$this->iniciarController($file, $controller, $action, $args);
       
	}

}

?>